//
//  WPNewReadingIndivalCaseCell.m
//  wobingwoyi
//
//  Created by WGP on 2016/11/28.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPNewReadingIndivalCaseCell.h"
#import "WPCredentialsCollectionViewCell.h"

#define NAME_FONT 16
#define INFO_FONT 14
@interface WPNewReadingIndivalCaseCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)UICollectionView *collectionView;

//@property(nonatomic,strong)UIImageView *starImageView;
//@property(nonatomic,strong)YYLabel *diseaseLabel;

@property(nonatomic,strong)UIImageView *lineView;

@end
@implementation WPNewReadingIndivalCaseCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.backgroundColor = GlobalBgColor;
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    [self myContentView];
    [self iconView];
    [self nameLabel];
    [self stateBtn];
    [self collectionView];
    [self descpLabel];
    [self diseaseBtn];
    [self hospitalLabel];
    [self doctorLabel];
    
    [self coinCount];
    [self coinImageView];
//
    [self lineView];
    [self eyeBtn];
    [self likeBtn];
    [self commitBtn];
    [self uploadTimeBtn];
}

-(UIView *)myContentView
{
    if (_myContentView == nil)
    {
        _myContentView = [UIView new];
        _myContentView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_myContentView];
        
        [_myContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(self.contentView).offset(5);
            make.bottom.equalTo(self.contentView).offset(-5);
            
        }];
        
    }
    return _myContentView;
}

-(UIImageView *)iconView
{
    if (_iconView == nil)
    {
        UIImage *image = [UIImage imageNamed:@"touxiang3"];
        _iconView = [[UIImageView alloc] initWithImage:image];
        [_iconView sizeToFit];
        
        _iconView.layer.cornerRadius = 15;
        _iconView.layer.masksToBounds = YES;
        
        [self.myContentView addSubview:_iconView];
        
        [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.myContentView).offset(15);
            make.top.equalTo(self.myContentView).offset(10);
            make.width.height.mas_equalTo(image.size.width);
        }];
    }
    return _iconView;
}

-(UILabel *)nameLabel
{
    if (_nameLabel == nil)
    {
        _nameLabel = [UILabel new];
        _nameLabel.text = @"昵称";
        _nameLabel.font = [UIFont systemFontOfSize:NAME_FONT];
        _nameLabel.text = @"孟小冬";
        //        _nameLabel.textColor = RGB_SAME(51);
        [_nameLabel sizeToFit];
        [self.myContentView addSubview:_nameLabel];
        
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconView.mas_right).offset(10);
            make.centerY.equalTo(self.iconView);
        }];
    }
    return _nameLabel;
}

-(UIButton *)stateBtn
{
    if (_stateBtn == nil)
    {
        _stateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_stateBtn setBackgroundImage:[UIImage imageNamed:@"stateBg_normal"] forState:UIControlStateNormal];
        [_stateBtn setTitle:@"以治愈" forState:UIControlStateNormal];
        _stateBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_stateBtn setTitleColor:RGB_SAME(102) forState:UIControlStateNormal];
        [self.myContentView addSubview:_stateBtn];
        
        [_stateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.iconView);
            make.right.equalTo(self.myContentView).offset(-10);
        }];
    }
    return _stateBtn;
}

-(UICollectionView *)collectionView
{
    if(_collectionView == nil)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.allowsSelection = NO;
        [_collectionView registerClass:[WPCredentialsCollectionViewCell class] forCellWithReuseIdentifier:@"WPCredentialsCollectionViewCell"];
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        CGFloat itemWidth = (SCREEN_WIDTH - 2*10-2*8)/3.0;
        CGFloat itemHeight = itemWidth*90/112.5;
        layout.itemSize = CGSizeMake(itemWidth, itemHeight);
        layout.minimumLineSpacing = 8;
        
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        [self.myContentView addSubview:_collectionView];
        
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.myContentView).offset(10);
            make.right.equalTo(self.myContentView);
            make.top.equalTo(self.iconView.mas_bottom).offset(10);
            make.height.mas_equalTo(itemHeight+2);
        }];
        
        UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecongnizer:)];
        [_collectionView addGestureRecognizer:tgr];
    }
    return _collectionView;
}

-(UILabel *)descpLabel
{
    if (_descpLabel == nil)
    {
        _descpLabel = [UILabel new];
        _descpLabel.font = [UIFont systemFontOfSize:INFO_FONT];
        _descpLabel.numberOfLines = 0;
        _descpLabel.textColor = RGB_SAME(102);
        [self.myContentView addSubview:_descpLabel];
        
        [_descpLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.collectionView);
            make.top.equalTo(self.collectionView.mas_bottom).offset(10);
            make.right.equalTo(self.myContentView).offset(-10);
        }];
    }
    return _descpLabel;
}

//start_icon

//-(UIImageView *)starImageView
//{
//    if(_starImageView == nil)
//    {
//        _starImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"start_icon"]];
//        [_starImageView sizeToFit];
//        [self.myContentView addSubview:_starImageView];
//        
//        [_starImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.collectionView);
//            make.top.equalTo(self.descpLabel.mas_bottom).offset(10);
//        }];
//    }
//    return _starImageView;
//}

//-(YYLabel *)diseaseLabel
//{
//    if (_diseaseLabel == nil)
//    {
//        _diseaseLabel = [YYLabel new];
//        [self.myContentView addSubview:_diseaseLabel];
//        
//        [_diseaseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.equalTo(self.starImageView);
//            make.left.equalTo(self.starImageView.mas_right).offset(10);
//        }];
//    }
//    return _diseaseLabel;
//}

-(UIButton *)diseaseBtn
{
    if (_diseaseBtn == nil)
    {
        _diseaseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _diseaseBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_diseaseBtn setImage:[UIImage imageNamed:@"start_icon"] forState:UIControlStateNormal];
        [_diseaseBtn addTarget:self action:@selector(diseaseBtnDidClick) forControlEvents:UIControlEventTouchDown];
        [_diseaseBtn setTitleColor:GlobalColor forState:UIControlStateNormal];
        [self.myContentView addSubview:_diseaseBtn];
        
        [_diseaseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.collectionView);
            make.top.equalTo(self.descpLabel.mas_bottom).offset(10);
        }];
    }
    return _diseaseBtn;
}

-(UIImageView *)lineView
{
    if (_lineView == nil)
    {
        _lineView = [UIImageView lineImageView];
        [self.myContentView addSubview:_lineView];
        
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconView);
            make.top.equalTo(self.diseaseBtn.mas_bottom).offset(10);
            make.height.mas_equalTo(0.5);
            make.right.equalTo(self.myContentView);
        }];
    }
    return _lineView;
}

-(UILabel *)hospitalLabel
{
    if(_hospitalLabel == nil)
    {
        _hospitalLabel = [UILabel new];
        _hospitalLabel.font = [UIFont systemFontOfSize:12];
        _hospitalLabel.textColor = RGB_SAME(102);
//        _hospitalLabel.text = @"犀利人民医院";
        [self.myContentView addSubview:_hospitalLabel];
        
        [_hospitalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.diseaseBtn);
            make.left.equalTo(self.diseaseBtn.mas_right).offset(10);
        }];
    }
    return _hospitalLabel;
}

-(UILabel *)doctorLabel
{
    if(_doctorLabel == nil)
    {
        _doctorLabel = [UILabel new];
        _doctorLabel.font = [UIFont systemFontOfSize:12];
        _doctorLabel.textColor = RGB_SAME(102);
        _doctorLabel.text = @"刘晓东医生";
        [self.myContentView addSubview:_doctorLabel];
        
        [_doctorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.diseaseBtn);
            make.left.equalTo(self.hospitalLabel.mas_right).offset(10);
        }];
    }
    return _doctorLabel;
}

-(UILabel *)coinCount
{
    if (_coinCount == nil)
    {
        _coinCount = [UILabel new];
        _coinCount.font = [UIFont systemFontOfSize:14];
        _coinCount.textColor = RGB_COLOR(255, 169,77);
        _coinCount.text = @"88";
        [_coinCount sizeToFit];
        [self.myContentView addSubview:_coinCount];
        
        [_coinCount mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.myContentView).offset(-10);
            make.centerY.equalTo(self.diseaseBtn);
        }];
    }
    return _coinCount;
}

-(UIImageView *)coinImageView
{
    
    if (_coinImageView == nil)
    {
        _coinImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"reading_coin"]];
        [_coinImageView sizeToFit];
        [self.myContentView addSubview:_coinImageView];
        
        [_coinImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.coinCount);
            make.right.equalTo(self.coinCount.mas_left).offset(-10);
        }];
    }
    return _coinImageView;
}

-(WPReadingCellBtn *)eyeBtn
{
    if (_eyeBtn == nil)
    {
        _eyeBtn = [WPReadingCellBtn buttonWithType:UIButtonTypeCustom];
        
        [_eyeBtn setImage:[UIImage imageNamed:@"kanguo"] forState:UIControlStateNormal];
//        _eyeBtn.backgroundColor = [UIColor orangeColor];
        [_eyeBtn setTitle:@"0" forState:UIControlStateNormal];
        [_eyeBtn setTitleColor:RGB_SAME(102) forState:UIControlStateNormal];
        _eyeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_eyeBtn.titleLabel sizeToFit];
        [self.myContentView addSubview:_eyeBtn];
        
        [_eyeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.lineView);
            make.top.equalTo(self.lineView.mas_bottom).offset(10);
            make.width.mas_equalTo(80);
        }];
        
    }
    return _eyeBtn;
}

-(WPReadingCellBtn *)likeBtn
{
    if (_likeBtn == nil)
    {
        _likeBtn = [WPReadingCellBtn buttonWithType:UIButtonTypeCustom];
        
        [_likeBtn setImage:[UIImage imageNamed:@"shoucang"] forState:UIControlStateNormal];
        [_likeBtn setTitle:@"0" forState:UIControlStateNormal];
        [_likeBtn setTitleColor:RGB_SAME(102) forState:UIControlStateNormal];
        _likeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [self.myContentView addSubview:_likeBtn];
        
        [_likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.eyeBtn);
            make.left.equalTo(self.eyeBtn).offset(80);
            make.width.mas_equalTo(80);
            
        }];
    }
    return _likeBtn;
}

-(WPReadingCellBtn *)commitBtn
{
    if (_commitBtn == nil)
    {
        _commitBtn = [WPReadingCellBtn buttonWithType:UIButtonTypeCustom];
        [_commitBtn setImage:[UIImage imageNamed:@"pinglun"] forState:UIControlStateNormal];
        [_commitBtn setTitle:@"0" forState:UIControlStateNormal];
        [_commitBtn setTitleColor:RGB_SAME(102) forState:UIControlStateNormal];
        _commitBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [self.myContentView addSubview:_commitBtn];
        
        [_commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.eyeBtn).offset(160);
            make.centerY.equalTo(self.eyeBtn);
            make.width.mas_equalTo(80);
        }];
    }
    return _commitBtn;
}

-(UIButton *)uploadTimeBtn
{
    if (_uploadTimeBtn == nil)
    {
        _uploadTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_uploadTimeBtn setImage:[UIImage imageNamed:@"shangchuan"] forState:UIControlStateNormal];
        [_uploadTimeBtn setTitle:@"  2015.06.30" forState:UIControlStateNormal];
        [_uploadTimeBtn setTitleColor:RGB_SAME(102) forState:UIControlStateNormal];
        _uploadTimeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [self.myContentView addSubview:_uploadTimeBtn];
        
        [_uploadTimeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.eyeBtn);
            make.right.equalTo(self.myContentView).offset(-10);
            make.bottom.equalTo(self.myContentView).offset(-10);
        }];
    }
    return _uploadTimeBtn;
}

-(void)setModel:(WPMRModel *)model
{
    _model = model;
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:model.userImage] placeholderImage:[UIImage imageNamed:@"touxiang3"]];
    self.nameLabel.text = model.userName;
    [self.stateBtn setTitle:model.finalCureStatus forState:UIControlStateNormal];
    self.descpLabel.text = model.firstOptionStr;
    
//    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:model.diseaseCategoryId];
//    text.yy_font = [UIFont systemFontOfSize:12];
//    text.yy_color = GlobalColor;
//    self.diseaseLabel.attributedText = text;
//    
//    __weak typeof(self) weakSelf = self;
//    self.diseaseLabel.textTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
//        
//        if ([weakSelf.delegate respondsToSelector:@selector(WPNewReadingIndivalCaseCellDiseaseDidClick:disease:)])
//        {
//            [weakSelf.delegate WPNewReadingIndivalCaseCellDiseaseDidClick:weakSelf disease:weakSelf.diseaseLabel.text];
//        }
//    };
    [self.diseaseBtn setTitle:[NSString stringWithFormat:@"  %@",model.name] forState:UIControlStateNormal];
    
    self.hospitalLabel.text = model.hospital;
    self.doctorLabel.text = model.doctor;
    
    if (model.payable)
    {
        self.coinCount.hidden = NO;
        self.coinImageView.hidden = NO;
        self.coinCount.text = [NSString stringWithFormat:@"%zd",model.pay];
    }
    else
    {
        self.coinCount.hidden = YES;
        self.coinImageView.hidden = YES;
    }
    
    [self.eyeBtn setTitle:[NSString stringWithFormat:@"%zd",model.readAmount] forState:UIControlStateNormal];
    [self.likeBtn setTitle:[NSString stringWithFormat:@"%zd",model.likeAmount] forState:UIControlStateNormal];
    [self.commitBtn setTitle:[NSString stringWithFormat:@"%zd",model.appraiseAmount] forState:UIControlStateNormal];
    NSString *timeStr = [[WPDateFormatterManager sharedManager] timeStrWithTimeStampStr:[NSString stringWithFormat:@"%zd",model.uploadTime] formatStr:@"yyyy.MM.dd"];
    [self.uploadTimeBtn setTitle:[NSString stringWithFormat:@"  %@",timeStr] forState:UIControlStateNormal];
    
    [self.collectionView reloadData];
}

-(void)tapGestureRecongnizer:(UITapGestureRecognizer *)tgr
{
    if (self.tapTriggered)
    {
        self.tapTriggered();
    }
}

#pragma mark - 按钮点击事件
-(void)diseaseBtnDidClick
{
    if ([self.delegate respondsToSelector:@selector(WPNewReadingIndivalCaseCellDiseaseDidClick:disease:)])
    {
        [self.delegate WPNewReadingIndivalCaseCellDiseaseDidClick:self disease:self.model.name];
    }
}

#pragma mark - collectionView的代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.model.firstOptionUrls.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WPCredentialsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WPCredentialsCollectionViewCell" forIndexPath:indexPath];
    cell.url = self.model.firstOptionUrls[indexPath.item];
    return cell;
}

@end
